//
//  SZLLoginViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/7/19.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLLoginViewController.h"
#import "SZLGetVerfyCodeViewController.h"
#import "LoginRegisterService.h"
#import "MBProgressHUD+MJ.h"
#import "LoginRegisterService.h"
#import <EMSDK.h>
#import "SZLAccountHelper.h"
#import "ChatDemoHelper.h"

@interface SZLLoginViewController ()
{
    BOOL isMovedBoard;
}
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *containViewTop;
@property (weak, nonatomic) IBOutlet UIView *containView;
@property (weak, nonatomic) IBOutlet UITextField *tellField;
@property (weak, nonatomic) IBOutlet UITextField *pswField;

@end

@implementation SZLLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self registerObserve];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self viewBack];
    self.navigationController.navigationBarHidden = YES;
}
- (IBAction)loginClick:(UIButton *)btn
{
    switch (btn.tag) {
        case 0:
        {
            [self login];
        }
            break;
        case 1:
        {
            [self registePsw:CodeTypeRegister];
        }
            break;
        case 2:
        {
            [self registePsw:CodeTypeForgetPsw];
        }
            break;
        case 3:
        {
            [self pop];
        }
            break;
        default:
            break;
    }
}
- (void)login
{
    if (self.tellField.text.length<1) {
        [MBProgressHUD showError:@"请输入手机号" toView:self.view];
        return;
    }
    if (self.pswField.text.length<1) {
        [MBProgressHUD showError:@"请输入密码" toView:self.view];
        return;
    }
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"mobilePhone"] = self.tellField.text;
    parame[@"loginPwd"] = self.pswField.text;
    [self showWaiter];
    [LoginRegisterService loginWithParame:parame response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            [MBProgressHUD showSuccess:@"登录成功" toView:self.view];
            //在此登录环信
//            [[EMClient sharedClient] loginWithUsername:[SZLAccountHelper shareInstance].mobilePhone password:[SZLAccountHelper shareInstance].hxPwd completion:^(NSString *aUsername, EMError *aError) {
//                if (!aError) {
//                    NSLog(@"环信登录成功");
//                    [[EMClient sharedClient].options setIsAutoLogin:YES];
//                    [[ChatDemoHelper shareHelper] asyncGroupFromServer];
//                    [[ChatDemoHelper shareHelper] asyncConversationFromDB];
//                    [[ChatDemoHelper shareHelper] asyncPushOptions];
//                }
//            }];
            //发通知给首页已经登录查询是否有新消息
            [[NSNotificationCenter defaultCenter]postNotificationName:UserLogined object:nil];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self dismissViewControllerAnimated:YES completion:nil];
            });
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)registePsw:(CodeType)type
{
    [self viewBack];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        SZLGetVerfyCodeViewController *registerVc = [[SZLGetVerfyCodeViewController alloc]initWithCodeType:type];
        [self.navigationController pushViewController:registerVc animated:YES];
    });
}

- (void)frameChanged:(NSNotification *)noti
{
    if (isMovedBoard) return;
    CGRect endRect = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    int curve = [noti.userInfo[UIKeyboardAnimationCurveUserInfoKey] intValue];
    if (endRect.origin.y == ScreenH) {
        [UIView animateWithDuration:duration delay:0 options:curve animations:^{
            self.containViewTop.constant = 0;
            [self.containView layoutIfNeeded];
        } completion:^(BOOL finished) {
            isMovedBoard = NO;
        }];
    }else{
        [UIView animateWithDuration:duration delay:0 options:curve animations:^{
            self.containViewTop.constant = -80;
            [self.containView layoutIfNeeded];
        } completion:^(BOOL finished) {
            isMovedBoard = YES;
        }];
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    isMovedBoard = NO;
    [self.view endEditing:YES];
}
- (void)pop
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewBack
{
    self.containViewTop.constant = 0;
    [self.view endEditing:YES];
}
- (void)registerObserve
{
    isMovedBoard = NO;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(frameChanged:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}
@end
